/* eslint-disable */
/**
 * Created by GYL on 2018/8/24
 */
//登录页面(工程中心返回中间页)
import React, { Component } from 'react';
import { connect } from 'dva';
import { Row, Col, Input, message, Icon, Modal } from 'antd';
import request from '../../utils/request';
import md5 from 'blueimp-md5';
import PublicService from '../../services/PublicService';
import styles from './login.less';
import 'antd/dist/antd.css';
import Base64 from 'base-64';
const { confirm } = Modal;
class Super extends Component {
  state = {
    enter: false,
    user: '',
    pwd: '',
    code: '',
    codeImg: '',
    autoLogin: false,
    loading: true,
    codeUrl: '/pm-web/projectmanage/login/getSignCode?t=',
    inputType: 'password'
  };
  componentDidMount() {

    var strName = this.props.location.search
    var strPassword = this.props.location.search

    var index = strName.lastIndexOf("?");
    var index3 = strName.lastIndexOf("name");
    var index4 = strName.lastIndexOf("password=");
    var index2 = strName.lastIndexOf("&");

    strPassword = strPassword.substr(index4 + 9, strPassword.length);
    strPassword = decodeURI(strPassword);

    strName = strName.substring(index3 + 5, index2);
    strName = decodeURI(strName);

    var decryptpass = Base64.decode(strName);
    var decryptpass1 = Base64.decode(strPassword);

    let statedata = {
      otherSystem: true,
      userName: decryptpass,
      password: decryptpass1
    }

    request({
      url: `/pm-web/projectmanage/login/login`,
      method: 'POST',
      data: statedata

    }).then(data => {
      // console.log(data)
      if (data.rc == 0) {
        if (data.ret.loggingStatus == 1) {
          confirm({
            title: '登录提示!',
            content: '该账号已有用户登录,是否重复登录?',
            okText: '确定',
            cancelText: '取消',
            onOk: () => {
              window.sessionStorage.removeItem('panes')
              // this.props.history.push("/App")

              this.checkLogin(data);
            },
            onCancel: () => {
              // window.location.href = 'http://47.114.47.224:9090/pm/#/logo';
              window.opener=null;
              window.open('','_self');
              window.close();
              // this.props.history.push("/logo")
              // this.setState({
              //   loading: false
              // });
              // this.refreshCode();
            },
          });
        } else {
          window.sessionStorage.removeItem('panes')
          // this.props.history.push("/App")

          this.checkLogin(data);
        }


      } else {

        this.setState({
          loading: false
        });
        this.refreshCode();
        message.warning(data.err);
      }
    }).catch((err) => {
      // console.log(err)
    })
  }


  // 判断是否登录成功
  checkLogin(data) {
    let t = this;
    this.setState({
      loading: false
    });
    // console.log(data)
    if (data.rc == 1) {
      message.warning(data.err);
    } else {
      PublicService.setCookie('userId', data.ret.userId);
      PublicService.setCookie('userName', data.ret.userName);
      PublicService.setCookie('nickName', data.ret.realName);
      PublicService.setCookie('logo', 2);
      window.location.href = 'https://47.114.47.224/pm/#/App';
      // window.location.href = 'http://192.168.0.109:8000/#/App'

      message.success('登录成功');
    }
  }




  render() {
    let t = this;
    let { loading } = t.state;
    return (
      <div className={styles["bg"]} onKeyUp={(e) => t.keyUp(e)}>
        <div className={styles.titlebox}>
          <img src={require("../../assets/titlelogo.png")} style={{ width: 70, height: 70, opacity: 1 }} />
          <div className={styles.text}>
            高能环境修复大脑平台
          </div>

        </div>

        <div className={styles['wp-content']}>

          {
            loading &&
            <div className={styles['wp-loading']}></div>
          }
        </div>
      </div>
    );
  }
}
function mapStateToProps(state) {
  return {}
}
export default connect(mapStateToProps)(Super)
